package net.csdn.business.common.oauth2.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.csdn.business.common.oauth2.model.entity.OauthClientInfo;
import net.csdn.business.common.domain.request.oauth.OauthClientInfoQuery;
import net.csdn.business.common.domain.vo.oauth.OauthClientInfoVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * oauth应用管理Mapper接口
 * 
 * @author zhangyl
 * @date 2022-11-07
 */
@Repository
public interface OauthClientInfoMapper  extends BaseMapper<OauthClientInfo>
{

    /**
     * 分页我创建的oauth应用列表
     *
     * @param query oauth应用管理详细信息
     * @return oauth应用管理
     */
    @Select("<script> select id, object_id, client_name, client_id, logo, client_desc, client_index, " +
            " web_server_redirect_uri, " +
            "created_by, created_date, last_modified_by, last_modified_date " +
            "from oauth_client_info where 1=1" +
            "<if test='query.createdBy != null  and query.createdBy != \"\"' >" +
            "AND created_by = #{query.createdBy}" +
            "</if>" +
            " </script>")
    public List<OauthClientInfoVo> findPageList(Page page, @Param("query") OauthClientInfoQuery query);


    /**
     * 分页查询已授权的oauth应用列表
     *
     * @param query oauth应用管理详细信息
     * @return oauth应用管理
     */
    @Select("<script> select ci.id, ci.object_id, ak.object_id as tokenId, ci.client_name, ci.client_id,ci.logo, ci.client_desc, ci.client_index, " +
            "ci.web_server_redirect_uri,  " +
            "ci.created_by, max(ak.created_date) as lastUseTime " +
            "from oauth_access_token ak inner join oauth_client_info ci on ci.client_id=ak.client_id " +
            "where ak.token_type=1 " +
            "AND ak.user_name = #{query.userName} " +
            "group by ci.client_id order by lastUseTime desc" +
            " </script>")
    public List<OauthClientInfoVo> findAuthorizeList(Page page, @Param("query") OauthClientInfoQuery query);


    /**
     * 查询oauth应用管理列表
     * 
     * @param query oauth应用管理
     * @return oauth应用管理集合
     */
    @Select("<script> select id, object_id, client_name, client_id, client_secret, logo, client_desc, client_index, resource_ids, scope, authorized_grant_types," +
            " web_server_redirect_uri, authorities, access_token_validity, refresh_token_validity, additional_information, " +
            "autoapprove, created_by, created_date, last_modified_by, last_modified_date " +
            "from oauth_client_info where 1=1" +
            "<if test='query.createdBy != null  and query.createdBy != \"\"' >" +
            "AND  created_by = #{query.createdBy}" +
            "</if>" +
            " </script>")
    public List<OauthClientInfoVo> selectOauthClientInfoList(@Param("query") OauthClientInfoQuery query);

}
